import Vue from 'vue'

import Vuex from 'vuex'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    //公共的变量，这里的变量不能随便修改，只能通过触发mutations的方法才能改变
    cart: uni.getStorageSync("cartList") ? JSON.parse(uni.getStorageSync("cartList")) : [],
    count: uni.getStorageSync("count") ? parseInt(uni.getStorageSync("count")) : 0,
    address: uni.getStorageSync("address") ? JSON.parse(uni.getStorageSync("address")) : {},
    checkCount: 0,
    checked: true,
    price: 0,
    token: uni.getStorageSync("token") ? JSON.parse(uni.getStorageSync("token")) : {},
    backInfo: ""
  },
  mutations: {
    //相当于同步的操作
    addCart(state, good) {
      const result = state.cart.find(item => item.goods_name === good.goods_name)
      if (!result) {
        state.cart.push(good)
      } else {
        result.goods_count++
      }
      uni.setStorageSync("cartList", JSON.stringify(state.cart))
    },
    changeCount(state) {
      let total = 0
      state.cart.forEach(item => {
        total += item.goods_count
      })
      state.count = total
      uni.setStorageSync("count", total)
    },
    RadioChange(state, data) {
      state.cart.forEach(item => {
        if (item.goods_id === data.id) {
          item.goods_state = !data.state
        }
      })
      uni.setStorageSync("cartList", JSON.stringify(state.cart))
    },
    numberChange(state, data) {
      state.cart.forEach(item => {
        if (item.goods_id === data.id) {
          item.goods_count = data.number
        }
      })
      uni.setStorageSync("cartList", JSON.stringify(state.cart))
    },
    deleteChange(state, id) {
      state.cart = state.cart.filter(item => item.goods_id !== id)
      uni.setStorageSync("cartList", JSON.stringify(state.cart))
    },
    setAddress(state, address) {
      state.address = address
    },
    setCheckCount(state) {
      let total = 0
      state.cart.forEach(item => {
        if (item.goods_state) {
          total += item.goods_count
        }
      })
      state.checkCount = total
    },
    setChecked(state) {
      let flag = true
      state.cart.forEach(item => {
        if (!item.goods_state) {
          flag = false
        }
      })
      if (flag) {
        state.checked = true
      } else {
        state.checked = false
      }
    },
    changeAllChecked(state) {
      state.checked = !state.checked
      state.cart.forEach(item => {
        item.goods_state = state.checked
      })
      uni.setStorageSync("cartList", JSON.stringify(state.cart))
    },
    setPrice(state) {
      let price = 0
      state.cart.forEach(item => {
        if (item.goods_state) {
          price += item.goods_count * item.goods_price
        }
      })
      state.price = price
    },
    setToken(state, token) {
      state.token = token
      uni.setStorageSync("token", JSON.stringify(token))
    },
    clearToken(state) {
      state.token = {}
    },
    setBackInfo(state, info) {
      state.backInfo = info
    },
    clearBackInfo(state) {
      state.backInfo = ""
    }
  },
  actions: {
    //相当于异步的操作,不能直接改变state的值，只能通过触发mutations的方法才能改变
  }
})
export default store